<?php

namespace App\Services;

/**
 * 接口分析数据
 * @package App\Services
 */
class WxtjInterface
{
    /**
     * 所有统计开始日期
     * @var string
     */
    private $allBeginDate;
    /**
     * 今天日期
     * @var false|string
     */
    private $todayDate;

    /**
     * 构造函数
     */
    public function __construct()
    {
        //全部拉取开始日期
        //$this->allBeginDate = '2014-12-01';
        $this->allBeginDate = date('Y').'-01-01';
        $this->todayDate = date('Y-m-d');
    }

    /**
     * 接口分析数据全部拉取
     * @return bool
     */
    public function pullAllSummary()
    {
        $beginDate = $this->allBeginDate;
        while ($beginDate < $this->todayDate){
            $endDate = date('Y-m-d', strtotime($beginDate.' +29 days'));
            if($endDate >= $this->todayDate ){
                $endDate = date('Y-m-d', strtotime($this->todayDate .' -1 days'));
            }
            //异步处理统计数据拉取
            \App\Jobs\WxtjInterfaceSummaryHandle::dispatch($beginDate, $endDate)->onQueue('low');
            if($endDate >= $this->todayDate ){
                $beginDate = date('Y-m-d', strtotime($endDate . '+2 days'));
            }else{
                $beginDate = date('Y-m-d', strtotime($endDate. ' +1 days'));
            }
        };
        return true;
    }
    /**
     * 接口分析数据拉取
     * @param string $beginDate
     * @param string $endDate
     * @return bool
     * @throws \Exception
     */
    public function pullSummary($beginDate = '', $endDate = '')
    {
        $beginDate || $beginDate = date('Y-m-d', strtotime('-30 days'));
        $endDate || $endDate = date('Y-m-d', strtotime('-1 days'));
        $app = \App\Services\EasyWechat::officialAccount();
        $apiRs = $app->data_cube->interfaceSummary($beginDate, $endDate);
        WxErrorService::checkApiResponse($apiRs);
        if (!empty($apiRs['list'])) {
            foreach ($apiRs['list'] as $v) {
                \App\Models\WxtjInterfaceSummary::updateOrCreate(
                    [
                        'ref_date' => $v['ref_date'],
                    ],
                    [
                        'callback_count' => $v['callback_count'] ?? 0,
                        'fail_count' => $v['fail_count'] ?? 0,
                        'total_time_cost' => $v['total_time_cost'] ?? 0,
                        'max_time_cost' => $v['max_time_cost'] ?? 0,
                    ]
                );
            }
        }
        return true;
    }

    /**
     * 接口分析分时数据全部拉取
     * @return bool
     */
    public function pullAllSummaryHour()
    {
        $beginDate = $this->allBeginDate;
        while ($beginDate < $this->todayDate){
            $endDate = date('Y-m-d', strtotime($beginDate.' +1 days'));
            if($endDate >= $this->todayDate ){
                $endDate = date('Y-m-d', strtotime($this->todayDate .' -1 days'));
            }
            //异步处理统计数据拉取
            \App\Jobs\WxtjInterfaceSummaryHandle::dispatch($beginDate, $endDate)->onQueue('low');
            if($endDate >= $this->todayDate ){
                $beginDate = date('Y-m-d', strtotime($endDate . '+2 days'));
            }else{
                $beginDate = date('Y-m-d', strtotime($endDate. ' +1 days'));
            }
        };
        return true;
    }
    /**
     * 接口分析分时数据拉取
     * @param string $beginDate
     * @param string $endDate
     * @return bool
     * @throws \Exception
     */
    public function pullSummaryHour($beginDate = '', $endDate = '')
    {
        $beginDate || $beginDate = date('Y-m-d', strtotime('-1 days'));
        $endDate || $endDate = date('Y-m-d', strtotime('-1 days'));
        $app = \App\Services\EasyWechat::officialAccount();
        $apiRs = $app->data_cube->interfaceSummaryHourly($beginDate, $endDate);
        WxErrorService::checkApiResponse($apiRs);
        if (!empty($apiRs['list'])) {
            foreach ($apiRs['list'] as $v) {
                \App\Models\WxtjInterfaceSummaryhour::updateOrCreate(
                    [
                        'ref_date' => $v['ref_date'],
                        'ref_hour' => $v['ref_hour']
                    ],
                    [
                        'callback_count' => $v['callback_count'] ?? 0,
                        'fail_count' => $v['fail_count'] ?? 0,
                        'total_time_cost' => $v['total_time_cost'] ?? 0,
                        'max_time_cost' => $v['max_time_cost'] ?? 0,
                    ]
                );
            }
        }
        return true;
    }
}